package com.lws.basic.utils.common;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.Map;

/**
 * 专门访问和设置SharePreference的工具类, 保存和配置一些设置信息
 *
 * @author Kevin
 */
public class SharePreferenceUtils {

    private static final String SHARE_PREFS_NAME = "lingshangyijia";

    public static boolean containsKey(Context ctx, String key) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);
        return pref.contains(key);
    }
    public static void removeKey(Context ctx, String key) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.remove(key);
        editor.apply();
    }



    public static void putBoolean(Context ctx, String key, boolean value) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        pref.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(Context ctx, String key,
                                     boolean defaultValue) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        return pref.getBoolean(key, defaultValue);
    }

    public static String getToken(Context ctx) {
        SharedPreferences pref = ctx.getSharedPreferences("lingshang_data",
            Context.MODE_PRIVATE);

        return pref.getString("token", "");
    }

    public static int getUserId(Context ctx) {
        SharedPreferences pref = ctx.getSharedPreferences("lingshang_data",
            Context.MODE_PRIVATE);

        return pref.getInt("ysid", 0);
    }



    public static void putString(Context context, String key, String value) {
        SharedPreferences pref = context.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        pref.edit().putString(key, value).commit();
    }

    /**
     * @param context  上下文
     * @param map   需要存储的集合
     *              将多个字符串存入  内部sd卡
     */
    public static void putStrings(Context context,Map<String,String> map) {
        SharedPreferences sharedPreferences = context.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            edit.putString(entry.getKey(),entry.getValue());
        }
        edit.commit();
    }

    public static String getString(Context ctx, String key, String defaultValue) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        return pref.getString(key, defaultValue);
    }


    public static void putInt(Context ctx, String key, int value) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        pref.edit().putInt(key, value).commit();
    }

    public static int getInt(Context ctx, String key, int defaultValue) {
        SharedPreferences pref = ctx.getSharedPreferences(SHARE_PREFS_NAME,
                Context.MODE_PRIVATE);

        return pref.getInt(key, defaultValue);
    }

    public static Map<String,?> getAll(Context context) {
        SharedPreferences preferences = context.getSharedPreferences(SHARE_PREFS_NAME, Context.MODE_PRIVATE);
        Map<String, ?> all = preferences.getAll();
        return all;
    }
}
